package com.str.graphs;
public class MatrixGraph implements Graph { // Graph: Adjacency matrix
  private int[][] matrix;  // The edge matrix
  private int numVertex;   // Number of vertices                
  private int numEdge;     // Number of edges

  public MatrixGraph(int num) {   // Constructor    
    matrix = new int[num][num];
    numVertex = num;
    numEdge = 0;
  }

  public int n() { return numVertex; }  // Number of vertices

  public int e() { return numEdge; }    // Number of edges

  public int first(int v) { // Get the first edge for a vertex
    int i;
    for (i=0; i<numVertex; i++)
      if (matrix[v][i] != 0) break;
    return i;    // Return n if none
  }

  public int next(int v1, int v2) { // Get next edge for a vertex
    int i;
    for (i=v2+1; i<numVertex; i++)
      if (matrix[v1][i] != 0) break;
    return i;    // Return n if none
  }

  public void setEdge(int v1, int v2, int wgt) { // Set edge weight
    if (wgt <= 0) {
      System.out.println("Illegal weight value");
      return;
    }
    if (matrix[v1][v2] == 0) numEdge++;
    matrix[v1][v2] = wgt;
  }

  public int weight(int v1, int v2)   // Return weight of edge
  { return matrix[v1][v2]; }
} // class GraphMatrix
